package com.nstar.countdown.business
{
	import com.nstar.base.util.Logger;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.remoting.mxml.RemoteObject;
	
	public class CountDownManager
	{
		public static const NEW = "A";
		public static const ENTERTAINMENT = "B";
		public static const MOVIE = "C";
		public static const MUSIC = "D";
		public static const SPORTS = "E";
		public static const CULTURE = "F";
		public static const ART = "G";
		public static const TRADTIONAL = "H";
		
		//远程对象池
		private var roPool:ArrayCollection = new ArrayCollection();
		private var logger:Logger = Logger.getInstance();
		
		public function CountDownManager(name:String="")
		{
			trace("CountDownManager "+name+" constructed!");
		}
		
		//检查池中是否已经有该方法调用所用的RemoteObject
		private function exist(id:String):RemoteObject{
			for each(var item in roPool){
				if(item.id == id){
					return item.remote as RemoteObject;
				}
			}
			return null;
		}
		
		//添加远程对象到池里
		private function add(id:String,remote:RemoteObject):void{
			var item:Object = new Object();
			item.id = id;
			item.remote = remote;
			roPool.addItem(item);
		}
		
		//取得远程对象，先从池子中找，没有则生成一个新的，methodName是用来区分同一地方调用多次的情况，返回数据的处理
		private function getRemoteObject(methodName:String,
			destination:String = "countDown"):RemoteObject{
			var ro:RemoteObject = exist(methodName);
			if(ro){
				logger.debug("find in pool");
				return ro;
			}else{
				logger.debug("new a RemoteObject and put into pool.");
				var roNew:RemoteObject = new RemoteObject();
				roNew.destination = destination;
				add(methodName,roNew);
				return roNew;
			}
		}
		
		private function clear():void{
			roPool.removeAll();
		}
		
		//分页查询
		public function findCountDown(filter:String,orderBy:String,start:int,end:int):RemoteObject{
			var countDown:RemoteObject = getRemoteObject("findCountDown");
			countDown.findCountDown(filter,orderBy,start,end);
			return countDown;
		}
		
		public function findCountDownTops(tops:int):RemoteObject{
			var countDown:RemoteObject = getRemoteObject("findCountDown");
			countDown.findCountDownTops(tops);
			return countDown;
		}		
		
		//根据类型查询
		public function findCountDownByHolidayTypeId(holidayTypeId:String):RemoteObject{
			var countDown:RemoteObject = getRemoteObject("findCountDownByHolidayTypeId("+holidayTypeId+")");
			countDown.findCountDownByHolidayTypeId(holidayTypeId);
			return countDown;
		}
		//根据标题查询
		public function findCountDownByTitle(title:String):RemoteObject{
			var countDown:RemoteObject = getRemoteObject("findCountDownByTitle("+title+")");
			countDown.findCountDownByTitle(title);
			return countDown;
		}		
		
		//根据标签查询
		public function findCountDownByTagId(tagId:String):RemoteObject{
			var countDown:RemoteObject = getRemoteObject("findCountDownByTagId("+tagId+")");
			countDown.findCountDownByTagId(tagId);
			return countDown;
		}		
		
		//插入新的倒计时数据
		public function insertCountDown(counDownJson:String,userId:String):RemoteObject{
			var countDown:RemoteObject = getRemoteObject("insertCountDown");
			countDown.insertCountDown(counDownJson,userId);
			return countDown;
		}
		
		//删除节日
		public function deleteCountDown(countDownId:String,userId:String):RemoteObject{
			var countDown:RemoteObject = getRemoteObject("deleteCountDown");
			countDown.deleteCountDown(countDownId,userId);
			return countDown;
		}
		
		//顶一下
		public function updateCountDownTops(countDownId:String):RemoteObject{
			var countDown:RemoteObject = getRemoteObject("updateCountDownTops");
			countDown.updateCountDownTops(countDownId);
			return countDown;
		}		
		
		//查询我的节日
		public function findMyCountDown(userId:String):RemoteObject{
			var countDown:RemoteObject = getRemoteObject("findMyCountDown");
			countDown.findMyCountDown(userId);
			return countDown;
		}
		
		//查询我的收藏
		public function findFavorite(userId:String):RemoteObject{
			var countDown:RemoteObject = getRemoteObject("findFavorite");
			countDown.findFavorite(userId);
			return countDown;
		}
		
		//添加收藏
		public function insertFavorite(countDownId:String,userId:String,
			date:Date,remarks:String):RemoteObject{
			var countDown:RemoteObject = getRemoteObject("insertFavorite");
			countDown.insertFavorite(countDownId,userId,date,remarks);
			return countDown;
		}
		
		//删除我的收藏
		public function deleteFavorite(countDownId:String,userId:String):RemoteObject{
			var countDown:RemoteObject = getRemoteObject("deleteFavorite");
			countDown.deleteFavorite(countDownId,userId);
			return countDown;
		}
		
		//查找标签
		public function findTagType(tops:int = 0):RemoteObject{
			var countDown:RemoteObject = getRemoteObject("findTagType");
			if(tops==0){
				countDown.findTagType();
			}else{
				countDown.findTagTypeTop(tops);
			}
			
			return countDown;
		}
	}
}